package com.raffles.oa.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.raffles.oa.dao.RafflesOADAOFactory;
import com.raffles.oa.domain.CompanyElectricCharge;
import com.raffles.oa.service.IElectricService;
import com.system.common.Page;
import com.system.util.BeanUtil;

public class ElectricServiceImpl implements IElectricService {

	public List<CompanyElectricCharge> queryPageListCompanyElectricCharge(CompanyElectricCharge bean, Page page) throws Exception {
		try {
			Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
			param.put("displayOrder", "A.COIN_CODE");
			List<CompanyElectricCharge> resultList = RafflesOADAOFactory.electricChargeDAO.queryPageListCompanyElectricCharge(param, page);
			
			for (CompanyElectricCharge companyElectricCharge : resultList) {
				String elinMeterNo = companyElectricCharge.getElinMeterNo();
				String coinCode = companyElectricCharge.getCoinCode();
				String elchPeriod = companyElectricCharge.getElchPeriod();
				
				elchPeriod = String.valueOf(Integer.parseInt(elchPeriod) - 1);
				CompanyElectricCharge companyElectricChargePreviousPeriod = RafflesOADAOFactory.electricChargeDAO.queryCompanyElectricCharge(coinCode, elinMeterNo, elchPeriod);
				companyElectricCharge.setCompanyElectricChargePreviousPeriod(companyElectricChargePreviousPeriod);
				
				//得到本月用电量差异数
				String currentElectricConsumption = companyElectricCharge.getElchConsumption();
				String previousElectricConsumption = "";
				if(!BeanUtil.isObjectNull(companyElectricChargePreviousPeriod)){
					previousElectricConsumption = companyElectricChargePreviousPeriod.getElchConsumption();
				}
				
				if(!BeanUtil.isBlank(currentElectricConsumption) && !BeanUtil.isBlank(previousElectricConsumption)){
					currentElectricConsumption = currentElectricConsumption.replaceFirst(",", "");
					previousElectricConsumption = previousElectricConsumption.replaceFirst(",", "");
					String elchVarience = getElectricVarience(currentElectricConsumption, previousElectricConsumption);
					companyElectricCharge.setElchVarience(elchVarience);
				}
			}
			
			return resultList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<CompanyElectricCharge> queryListCompanyElectricCharge(CompanyElectricCharge bean) throws Exception {
		try {
			Map<String, Object> param = BeanUtil.convertBeanToMap(bean);
			param.put("displayOrder", "A.COIN_CODE");
			List<CompanyElectricCharge> resultList = RafflesOADAOFactory.electricChargeDAO.queryListCompanyElectricCharge(param);
			
			return resultList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 
	 * 公式：
	 * (本月用电量 - 上月用电量)/上月用电量
	 */
	public String getElectricVarience(String currentElectricConsumption, String previousElectricConsumption) throws Exception {
		try {
			String varience = "";
			BigDecimal cur = new BigDecimal(currentElectricConsumption);//本月用电量
			BigDecimal pre = new BigDecimal(previousElectricConsumption);//上月用电量
			BigDecimal subtract = cur.subtract(pre);//本月用电量 减去 上月用电量
			pre = new BigDecimal(pre.doubleValue());
			
			if(pre.intValue()!=0){
				String varienceD = String.valueOf(subtract.divide(pre, 4, BigDecimal.ROUND_HALF_UP).doubleValue() * 100);
				BigDecimal varienceB = new BigDecimal(varienceD);
				varience = varienceB.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "%";
			}
			
			return varience;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	

}
